package com.Daisy.swi.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.DateFormat;
import java.text.ParseException;

import com.Daisy.swi.client.Data.Book;
import com.Daisy.swi.client.Data.BookList;
import com.Daisy.swi.client.Data.Manager;
import com.Daisy.swi.client.Data.Reader;
import com.Daisy.swi.client.Data.ReaderList;
import com.Daisy.swi.client.Data.Teacher;

public class ClientLaunch {
	private Socket socket;
	private PrintWriter sessionWriter;
	private BufferedReader reader;
	private final String SUCCESS = "success";
	private final String TRUE = "true";

	public ClientLaunch() {
		setUpNetWorking();
	}

	private void setUpNetWorking() {
		try {
			socket = new Socket("127.0.0.1", 4242);
			InputStreamReader isReader = new InputStreamReader(
					socket.getInputStream());
			reader = new BufferedReader(isReader);
			sessionWriter = new PrintWriter(socket.getOutputStream());
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public Reader readerLogin(String id, String password) {
		Reader r = null;
		String message;
		sessionWriter.println("readerLogin");
		sessionWriter.println(id);
		sessionWriter.println(password);
		sessionWriter.flush();
		try {
			message = reader.readLine();
			if (message.equals(SUCCESS)) {
				r = readReader();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return r;
	}

	public void giveBackBook(String idOfReader, int indexOfBook) {
		sessionWriter.println("giveBackBook");
		sessionWriter.println(idOfReader);
		sessionWriter.println(indexOfBook);
		sessionWriter.flush();
	}

	public String borrowBookedBook(String idOfTeacher, int indexOfBook) {
		sessionWriter.println("borrowBookedBook");
		sessionWriter.println(idOfTeacher);
		sessionWriter.println(indexOfBook);
		sessionWriter.flush();
		String message = "";
		try {
			message = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return message;
	}

	public void cancelBooking(String idOfTeacher, int indexOfBook) {
		sessionWriter.println("cancelBooking");
		sessionWriter.println(idOfTeacher);
		sessionWriter.println(indexOfBook);
		sessionWriter.flush();
	}

	public String reBorrow(String idOfReader, int indexOfBook) {
		sessionWriter.println("reBorrow");
		sessionWriter.println(idOfReader);
		sessionWriter.println(indexOfBook);
		sessionWriter.flush();
		String message = "";
		try {
			message = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return message;
	}

	public void booking(int idOfTeacher, int indexOfBook, int idOfStudent) {
		sessionWriter.println("booking");
		sessionWriter.println(idOfTeacher);
		sessionWriter.println(indexOfBook);
		sessionWriter.println(idOfStudent);
		sessionWriter.flush();
	}

	public String borrow(String idOfReader, int indexOfBook) {
		sessionWriter.println("borrow");
		sessionWriter.println(idOfReader);
		sessionWriter.println(indexOfBook);
		sessionWriter.flush();

		String message = "";
		try {
			message = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return message;
	}

	public BookList readerSearchABook(String idOfReader, int searchingType,
			String searchingStr) {
		sessionWriter.println("readerSearchABook");
		sessionWriter.println(idOfReader);
		sessionWriter.println(searchingType);
		sessionWriter.println(searchingStr);
		sessionWriter.flush();

		String message = "";
		BookList rightBooks = new BookList();
		try {
			message = reader.readLine();
			if (message.equals(SUCCESS)) {
				rightBooks = readBooks();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return rightBooks;
	}

	public BookList managerSearchABook(int searchingType, String searchingStr) {
		sessionWriter.println("managerSearchABook");
		sessionWriter.println(searchingType);
		sessionWriter.println(searchingStr);
		sessionWriter.flush();

		String message = "";
		BookList rightBooks = new BookList();
		try {
			message = reader.readLine();
			if (message.equals(SUCCESS)) {
				rightBooks = readBooks();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return rightBooks;
	}

	public ReaderList searchAReader(int searchingType, String searchingStr) {
		sessionWriter.println("searchAReader");
		sessionWriter.println(searchingType);
		sessionWriter.println(searchingStr);
		sessionWriter.flush();

		String message = "";
		ReaderList rightReaders = new ReaderList();
		try {
			message = reader.readLine();
			if (message.equals(SUCCESS)) {
				rightReaders = readReaders();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return rightReaders;
	}

	public BookList searchAllBooks() {
		sessionWriter.println("searchAllBooks");
		sessionWriter.flush();

		BookList allBooks = new BookList();
		try {
			String message = "";
			message = reader.readLine();
			if (message.equals(SUCCESS)) {
				allBooks = readBooks();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return allBooks;
	}

	public ReaderList searchAllReaders() {
		sessionWriter.println("searchAllReaders");
		sessionWriter.flush();

		ReaderList allReaders = new ReaderList();
		try {
			String message = "";
			message = reader.readLine();
			if (message.equals(SUCCESS)) {
				allReaders = readReaders();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return allReaders;
	}

	public void deleteBook(int indexOfBook) {
		sessionWriter.println("deleteBook");
		sessionWriter.println(indexOfBook);
		sessionWriter.flush();
	}

	public void deleteReader(int indexOfReader) {
		sessionWriter.println("deleteReader");
		sessionWriter.println(indexOfReader);
		sessionWriter.flush();
	}

	public void resetBook(String name, String writer, String publisher,
			int publishYear, String key, String location, int type, int num,
			String description, int indexOfBook) {
		sessionWriter.println("resetBook");
		sessionWriter.println(name);
		sessionWriter.println(writer);
		sessionWriter.println(publisher);
		sessionWriter.println(publishYear);
		sessionWriter.println(key);
		sessionWriter.println(location);
		sessionWriter.println(type);
		sessionWriter.println(num);
		sessionWriter.println(description);
		sessionWriter.println(indexOfBook);
		sessionWriter.flush();
	}

	public Manager managerLogin(String id, String password) {
		Manager manager = null;
		String message;
		sessionWriter.println("managerLogin");
		sessionWriter.println(id);
		sessionWriter.println(password);
		sessionWriter.flush();
		try {
			message = reader.readLine();
			if (message.equals(SUCCESS)) {
				String name = reader.readLine();
				manager = new Manager(name, id, password);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return manager;
	}

	public boolean resetReader(String name, String newID, String id,
			String password, String institute, int type) {
		sessionWriter.println("resetReader");
		sessionWriter.println(name);
		sessionWriter.println(newID);
		sessionWriter.println(id);
		sessionWriter.println(password);
		sessionWriter.println(institute);
		sessionWriter.println(type);
		sessionWriter.flush();

		String message;
		boolean finish = false;
		try {
			message = reader.readLine();
			if (message.equals(SUCCESS)) {
				finish = true;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return finish;
	}

	public boolean addAReader(String name, String id, String password, int type) {
		sessionWriter.println("addAReader");
		sessionWriter.println(name);
		sessionWriter.println(id);
		sessionWriter.println(password);
		sessionWriter.println(type);
		sessionWriter.flush();

		String message;
		boolean finish = false;
		try {
			message = reader.readLine();
			if (message.equals(SUCCESS)) {
				finish = true;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return finish;
	}

	public boolean exit() {
		sessionWriter.println("exit");
		sessionWriter.flush();

		String message;
		boolean finish = false;
		try {
			message = reader.readLine();
			if (message.equals(SUCCESS)) {
				finish = true;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			reader.close();
			sessionWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return finish;
	}

	public void addABook(String name, String writer, String publisher,
			int publishYear, String key, String location, int num, int type, 
			String description) {
		sessionWriter.println("addABook");
		sessionWriter.println(name);
		sessionWriter.println(writer);
		sessionWriter.println(publisher);
		sessionWriter.println(publishYear);
		sessionWriter.println(key);
		sessionWriter.println(location);
		sessionWriter.println(num);
		sessionWriter.println(type);
		sessionWriter.println(description);
		sessionWriter.flush();
	}

	public BookList readBooks() {
		BookList books = new BookList();
		try {
			int size = Integer.parseInt(reader.readLine());
			if (size != 0) {
				for (int i = 0; i < size; i++) {
					Book book = readBook();
					books.add(book);
				}
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return books;
	}

	public Book readBook() {
		Book book = null;
		try {
			String name = reader.readLine();
			String writer = reader.readLine();
			String publisher = reader.readLine();
			int publishYear = Integer.parseInt(reader.readLine());
			String key = reader.readLine();
			String location = reader.readLine();
			int num = Integer.parseInt(reader.readLine());
			int type = Integer.parseInt(reader.readLine());
			String description = reader.readLine();
			String barCode = reader.readLine();
			int status = Integer.parseInt(reader.readLine());
			String backDate = reader.readLine();

			book = new Book();
			book.setName(name);
			book.setWriter(writer);
			book.setPublisher(publisher);
			book.setPublishYear(publishYear);
			book.setKey(key);
			book.setLocation(location);
			book.setNum(num);
			book.setType(type);
			book.setDescription(description);
			book.setBarCode(barCode);
			book.setStatus(status);
			if (!backDate.equals("hello")) {
				book.setBackDate(DateFormat.getDateInstance().parse(backDate));
			}
			String isAsked = reader.readLine();
			if (isAsked.equals(TRUE)) {
				book.beAskedFor();
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return book;
	}

	public ReaderList readReaders() {
		ReaderList readers = new ReaderList();
		try {
			int size = Integer.parseInt(reader.readLine());
			if (size != 0) {
				for (int i = 0; i < size; i++) {
					Reader reader = readReader();
					readers.add(reader);
				}
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return readers;
	}

	public Reader readReader() {
		Reader r = null;
		try {
			String name = reader.readLine();
			String id = reader.readLine();
			String password = reader.readLine();
			String institute = reader.readLine();
			int type = Integer.parseInt(reader.readLine());
			
			if (type == 3) {
				Teacher t = new Teacher(name, id, password, institute, type);
				BookList myBooks = readBooks();
				t.setBooks(myBooks);
				BookList bookedBooks = readBooks();
				t.setBookedBook(bookedBooks);
				r = t;
			} else {
				r = new Reader(name, id, password, institute, type);
				BookList myBooks = readBooks();
				r.setBooks(myBooks);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return r;
	}

	public void clearAddingBooks() {
		sessionWriter.println("clearAddingBooks");
		sessionWriter.flush();
	}

	public boolean lendBook(String barCode, String readerId) {
		sessionWriter.println("helpReaderBorrow");
		sessionWriter.println(barCode);
		sessionWriter.println(readerId);
		sessionWriter.flush();
		
		String message = "";
		try {
			message = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return message.equals(SUCCESS);
	}

	public boolean gevieBackBook(String barCode, String readerId) {
		sessionWriter.println("helpReaderReturn");
		sessionWriter.println(barCode);
		sessionWriter.println(readerId);
		sessionWriter.flush();
		
		String message = "";
		try {
			message = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return message.equals(SUCCESS);
	}
}
